﻿/********************************************************************************
* Author:	Christos Polydorou	(xristos.polydoroy@gmail.com)					*
* Author:	Aris Michail		(aris.michail@gmail.com)						*
* Date:		22/9/2011															*
********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication
{

    public partial class PropertyUpdate : System.Web.UI.Page
    {
        Property q = new Property();
        DataClassesDataContext dc;
        String id = "";
        long idd = 0;
        Property prop = new Property();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Session["isValid"].ToString().Equals("true"))
                    Response.Redirect("login.aspx");
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                Response.Redirect("login.aspx");
            }

            dc = new DataClassesDataContext();
            id = Request.QueryString["property"];
            try
            {
                idd = long.Parse(id);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                Response.Redirect("errorpage.aspx");
            }
            // Get the property
            q =
                (from p in dc.Properties
                 where p.ID == idd
                 select p).Single();
            //Update page controls
            if( this.q.ProtocolNumber != null )
                this.ProtoNum.Text = q.ProtocolNumber;

            if (q.PropertyNomos != null)
                if (!Session["usertype"].Equals("adm"))
                    this.DropDownList1.Enabled = false;
            if (q.PropertyDimos != null)
                if (!Session["usertype"].Equals("adm"))
                    this.DropDownList2.Enabled = false;

            DropDownList1_Populate();

            if (!IsPostBack)
            {
                if (this.DropDownList1.SelectedValue.Equals("aa"))
                    this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                else
                {
                    this.DropDownList2.DataSource = from dimos in dc.Dimois
                                                    where dimos.Nomos == this.DropDownList1.SelectedValue
                                                    orderby dimos.Onoma
                                                    select new { name = dimos.Onoma };
                    this.DropDownList2.DataTextField = "name";
                    this.DropDownList2.DataValueField = "name";
                    this.DropDownList2.DataBind();
                    this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                    if (!this.q.PropertyDimos.Equals(""))
                        this.DropDownList2.SelectedValue = this.q.PropertyDimos;
                }

            }

            if (!IsPostBack)
            {

                if (q.PropertySurface != null)
                {
                    this.TextBox1.Text = q.PropertySurface.ToString();
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox1.ReadOnly = true;
                }
                if (q.PropertyArea != null)
                {
                    if (!q.PropertyArea.Equals(""))
                    {
                        this.TextBox4.Text = q.PropertyArea;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox4.ReadOnly = true;
                    }
                }
                if (q.OSDENumber != null)
                {
                    this.TextBox5.Text = q.OSDENumber.ToString();
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox5.ReadOnly = true;
                }
                if (q.AnadasmosNumber != null)
                {
                    this.TextBox6.Text = q.AnadasmosNumber.ToString();
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox6.ReadOnly = true;
                }
                if (q.RegistrationOffice != null)
                {
                    if (!q.RegistrationOffice.Equals(""))
                    {
                        this.TextBox7.Text = q.RegistrationOffice;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox7.ReadOnly = true;
                    }
                }
                if (q.OwnerName != null)
                {
                    if (!q.OwnerName.Equals(""))
                    {
                        this.TextBox8.Text = q.OwnerName;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox8.ReadOnly = true;
                    }
                }

                if (q.OwnerAddress != null)
                {
                    if (!q.OwnerAddress.Equals(""))
                    {
                        this.TextBox9.Text = q.OwnerAddress;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox9.ReadOnly = true;
                    }
                }

                if (q.OwnerTF != null)
                {
                    if (!q.OwnerTF.Equals(""))
                    {
                        this.TextBox10.Text = q.OwnerTF;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox10.ReadOnly = true;
                    }
                }
                if (q.FirstSeedKodikosSporou != null)
                {
                    if (!q.FirstSeedKodikosSporou.Equals(""))
                    {
                        this.SeedCode1.Text = q.FirstSeedKodikosSporou;
                        if (!Session["usertype"].Equals("adm"))
                            this.SeedCode1.ReadOnly = true;
                    }
                }
                if (q.FirstSeedSeed != null)
                {
                    //this.TextBox11.Text = q.FirstSeedSeed;
                    this.TextBox11.Text = q.FirstSeedSeed.ToString();

                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox11.ReadOnly = true;
                }

                if (q.SecondSeedSeed != null)
                {
                    this.TextBox12.Text = q.SecondSeedSeed.ToString();
                    if (!Session["usertype"].Equals("adm"))
                        this.TextBox12.ReadOnly = true;
                }
                if (q.SecondSeedKodikosSporou != null)
                {
                    if (!q.SecondSeedKodikosSporou.Equals(""))
                    {
                        this.SeedCode2.Text = q.SecondSeedKodikosSporou;
                        if (!Session["usertype"].Equals("adm"))
                            this.SeedCode2.ReadOnly = true;
                    }
                }
                if (q.Coordinates != null)
                {
                    if (!q.Coordinates.Equals(""))
                    {
                        this.Coordinates.Text = q.Coordinates;
                        if (!Session["usertype"].Equals("adm"))
                            this.Coordinates.ReadOnly = true;
                    }
                }
                if (q.Apopsigeoponou != null)
                {
                    if (!q.Apopsigeoponou.Equals(""))
                    {
                        this.TextBox13.Text = q.Apopsigeoponou;
                        if (!Session["usertype"].Equals("adm"))
                            this.TextBox13.ReadOnly = true;
                    }
                }
            }

            //Hmerologia me postback i xoris?
            if (!IsPostBack)
            {
                if (q.SignatureDate != null)
                {
                    this.Calendar1.SelectedDate = (DateTime)q.SignatureDate;
                    // if (!Session["usertype"].Equals("adm"))
                    //   this.Calendar1.ReadOnly = true;
                }

                if (q.ReceivedDate != null)
                    this.Calendar2.SelectedDate = (DateTime)q.ReceivedDate;

                if (q.FirstSeedDate != null)
                    this.Calendar3.SelectedDate = (DateTime)q.FirstSeedDate;

                if (q.SecondSeedDate != null)
                    this.Calendar4.SelectedDate = (DateTime)q.SecondSeedDate;

                if (q.ApopsigeoponouDate != null)
                    this.Calendar5.SelectedDate = (DateTime)q.ApopsigeoponouDate;

            }

        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            if ((this.q.SignatureDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
            {
                this.q.SignatureDate = this.Calendar1.SelectedDate;
            }
            else
                this.Calendar1.SelectedDate = (DateTime)this.q.SignatureDate;
        }
        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            if ((this.q.ReceivedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
            {
                this.q.ReceivedDate = this.Calendar2.SelectedDate;
            }
            else
                this.Calendar2.SelectedDate = (DateTime)this.q.ReceivedDate;
        }
        protected void Calendar3_SelectionChanged(object sender, EventArgs e)
        {
            if ((this.q.FirstSeedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
            {
                this.q.FirstSeedDate = this.Calendar3.SelectedDate;
            }
            else
                this.Calendar3.SelectedDate = (DateTime)this.q.FirstSeedDate;
        }
        protected void Calendar4_SelectionChanged(object sender, EventArgs e)
        {
            if ((this.q.SecondSeedDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
            {
                this.q.SecondSeedDate = this.Calendar4.SelectedDate;
            }
            else
                this.Calendar4.SelectedDate = (DateTime)this.q.SecondSeedDate;
        }
        protected void Calendar5_SelectionChanged(object sender, EventArgs e)
        {
            if ((this.q.ApopsigeoponouDate.ToString().Equals("")) || (Session["usertype"].Equals("adm")))
            {
                this.q.ApopsigeoponouDate = this.Calendar5.SelectedDate;
            }
            else
                this.Calendar5.SelectedDate = (DateTime)this.q.ApopsigeoponouDate; ;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            GridViewExportUtil.Export("SearchResults.xls", this.GridView2);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (q.PropertyNomos != null)
            {
                if ((this.q.PropertyNomos.Equals("")) || (Session["usertype"].Equals("adm")))
                {
                    if (!this.DropDownList1.SelectedValue.Equals("aa"))
                        this.q.PropertyNomos = this.DropDownList1.SelectedItem.Value;
                    DropDownList2_Populate();
                    this.Label1.Text = "Παρακαλώ επιλέξτε τον νέο δήμο ";
                }
                else
                    this.DropDownList1.SelectedValue = this.q.PropertyNomos;
            }
            if (!this.DropDownList1.SelectedValue.Equals("aa"))
                this.q.PropertyNomos = this.DropDownList1.SelectedValue;
            DropDownList2_Populate();
        }

        protected void DropDownList1_Populate()
        {
            if ((this.DropDownList1.SelectedValue.Equals("aa")) || (this.DropDownList1.SelectedValue.Equals("")))
            {
                this.DropDownList1.DataSource = from nomos in dc.Nomois
                                                orderby nomos.Onoma
                                                select new { Onoma = nomos.Onoma };

                this.DropDownList1.DataTextField = "Onoma";
                this.DropDownList1.DataValueField = "Onoma";
                this.DropDownList1.DataBind();

                if (q.PropertyNomos != null)
                {
                    this.DropDownList1.SelectedValue = q.PropertyNomos;
                }
                else
                {
                    this.DropDownList1.Items.Insert(0, new ListItem("Επιλογή Νομού", "aa"));
                }
            }
        }


        protected void DropDownList2_Populate()
        {

            if ((this.q.PropertyDimos != null) || (HttpContext.Current.Session["usertype"].Equals("adm")))
            {

                if ((!this.DropDownList1.SelectedValue.Equals("aa")))
                {
                    if (!this.DropDownList1.SelectedValue.Equals(""))
                    {
                        this.DropDownList2.DataSource = from dimos in dc.Dimois
                                                        where dimos.Nomos == this.DropDownList1.SelectedValue
                                                        orderby dimos.Onoma
                                                        select new { name = dimos.Onoma };
                        this.DropDownList2.DataTextField = "name";
                        this.DropDownList2.DataValueField = "name";
                        this.DropDownList2.DataBind();
                        this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                    }
                }
                else
                {
                    this.DropDownList2.Items.Insert(0, new ListItem("Επιλογή Δήμου", "bb"));
                }
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Label1.Text = "";
            if (!(this.DropDownList2.SelectedValue.Equals("bb")) || (Session["usertype"].Equals("adm")))
            {
                this.q.PropertyDimos = this.DropDownList2.SelectedItem.Value;
            }
            else
            {
                this.DropDownList2.SelectedValue = this.q.PropertyDimos;
            }

            if (!this.DropDownList2.SelectedValue.Equals("bb"))
                this.q.PropertyDimos = this.DropDownList2.SelectedValue;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue.Equals("aa"))
            {
                q.PropertyNomos = null;
            }
            else
                q.PropertyNomos = DropDownList1.SelectedValue;
            if (DropDownList2.SelectedValue.Equals("bb"))
            {
                q.PropertyDimos = null;
            }
            else
                q.PropertyDimos = DropDownList2.SelectedValue;
            if (!this.TextBox1.Text.Equals(""))
            {
                try
                {
                    this.q.PropertySurface = double.Parse(this.TextBox1.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Επιφάνεια"" μόνο με αριθμούς";
                    return;
                }
            }
            if (!this.TextBox4.Text.Equals(""))
            {
                this.q.PropertyArea = this.TextBox4.Text;
            }
            if (!this.TextBox5.Text.Equals(""))
            {
                try
                {
                    this.q.OSDENumber = long.Parse(this.TextBox5.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός ΟΣΔΕ"" μόνο με αριθμούς";
                    return;
                }
            }
            if (!TextBox6.Text.Equals(""))
            {
                try
                {
                    this.q.AnadasmosNumber = long.Parse(this.TextBox6.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""Αριθμός Αναδασμού"" μόνο με αριθμούς";
                    return;
                }
            }
            if (!this.TextBox7.Text.Equals(""))
            {
                this.q.RegistrationOffice = this.TextBox7.Text;
            }
            if (!this.SeedCode1.Text.Equals(""))
            {
                this.q.FirstSeedKodikosSporou = this.SeedCode1.Text;
            }
            if (!this.SeedCode2.Text.Equals(""))
            {
                this.q.SecondSeedKodikosSporou = this.SeedCode2.Text;
            }
            if (!this.Coordinates.Text.Equals(""))
            {
                this.q.Coordinates = this.Coordinates.Text;
            }
            if (!this.TextBox8.Text.Equals(""))
            {
                this.q.OwnerName = this.TextBox8.Text;
            }
            if (!this.TextBox9.Text.Equals(""))
            {
                this.q.OwnerAddress = this.TextBox9.Text;
            }
            if (!this.TextBox10.Text.Equals(""))
            {
                this.q.OwnerTF = this.TextBox10.Text;
            }
            if (!this.TextBox11.Text.Equals(""))
            {
                try
                {
                    q.FirstSeedSeed = double.Parse(TextBox11.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""1η Σπορά-Σπόρος"" μόνο με αριθμούς";
                    return;
                }
            }
            if (!this.TextBox12.Text.Equals(""))
            {
                try
                {
                    q.SecondSeedSeed = double.Parse(TextBox12.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = @"Συμπληρώστε το πεδίο ""2η Σπορά-Σπόρος"" μόνο με αριθμούς";
                    return;
                }
            }
            if (Calendar1.SelectedDate.Year != 1)
            {
                q.SignatureDate = Calendar1.SelectedDate;
            }
            if (Calendar2.SelectedDate.Year != 1)
            {
                q.ReceivedDate = Calendar2.SelectedDate;
            }
            if (Calendar3.SelectedDate.Year != 1)
            {
                q.FirstSeedDate = Calendar3.SelectedDate;
            }
            if (Calendar4.SelectedDate.Year != 1)
            {
                q.SecondSeedDate = Calendar4.SelectedDate;
            }
            if (Calendar5.SelectedDate.Year != 1)
            {
                q.ApopsigeoponouDate = Calendar5.SelectedDate;
            }

            if (TextBox13.Text.Length > 0)
                q.Apopsigeoponou = TextBox13.Text;

            this.q.LastUpdatedBy = HttpContext.Current.Session["username"].ToString();

            CreateProtocolNumber();

            try
            {
                this.dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                HttpContext.Current.Session["message"] = "Η ενημέρωση απέτυχε";
                return;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            PropertyHistory history = new PropertyHistory();

            if (e.CommandName.Equals("Insert"))
            {
                TextBox YearDate = GridView1.FooterRow.FindControl("year") as TextBox;
                TextBox OfficeSup = GridView1.FooterRow.FindControl("officesupervisor") as TextBox;
                TextBox FieldSup = GridView1.FooterRow.FindControl("fieldsupervisor") as TextBox;
                TextBox agriculturist = GridView1.FooterRow.FindControl("agriculturist") as TextBox;
                TextBox agriculturistapopsi = GridView1.FooterRow.FindControl("agriculturistapopsi") as TextBox;
                TextBox mixanima1 = GridView1.FooterRow.FindControl("mixanimatherismou1") as TextBox;
                TextBox tfmixanima1 = GridView1.FooterRow.FindControl("tfmixanimatos1") as TextBox;
                TextBox mixanima2 = GridView1.FooterRow.FindControl("mixanimatherismou2") as TextBox;
                TextBox tfmixanima2 = GridView1.FooterRow.FindControl("tfmixanimatos2") as TextBox;
                TextBox production = GridView1.FooterRow.FindControl("production") as TextBox;
                TextBox productionkindseed = GridView1.FooterRow.FindControl("productionkindseed") as TextBox;
                TextBox productionquantity = GridView1.FooterRow.FindControl("productionquantity") as TextBox;
                TextBox productionbuyprice = GridView1.FooterRow.FindControl("productionbuyprice") as TextBox;
                Calendar agriculturistApopsidate = GridView1.FooterRow.FindControl("agriculturistApopsidate") as Calendar;
                Calendar HarvestDate = GridView1.FooterRow.FindControl("harvestdate") as Calendar;
                TextBox productionquantitymass = GridView1.FooterRow.FindControl("productionquantitymass") as TextBox;
                TextBox productionbuypricemass = GridView1.FooterRow.FindControl("productionbuypricemass") as TextBox;
                TextBox productionkindmass = GridView1.FooterRow.FindControl("productionkindmass") as TextBox;
                TextBox seedcode = GridView1.FooterRow.FindControl("SeedCodeText") as TextBox;

                history.PropertyID = idd;
                if (!YearDate.Text.Equals(""))
                    history.YearDate = int.Parse(YearDate.Text);
                else
                {
                    HttpContext.Current.Session["message"] = "Παρακαλώ εισάγετε το έτος του ιστορικού";
                    return;
                }
                if (!seedcode.Text.Equals(""))
                    history.KodikosSporou = seedcode.Text;
                if (!OfficeSup.Text.Equals(""))
                    history.OfficeSupervisor = OfficeSup.Text;
                if (!FieldSup.Text.Equals(""))
                    history.FieldSupervisor = FieldSup.Text;
                if (!agriculturist.Text.Equals(""))
                    history.agriculturist = agriculturist.Text;
                if (!agriculturistapopsi.Text.Equals(""))
                    history.agriculturistApopsi = agriculturistapopsi.Text;
                if (!mixanima1.Text.Equals(""))
                    history.MixanimaTherismou1 = mixanima1.Text;
                if (!tfmixanima1.Text.Equals(""))
                    history.TFMixanimatos1 = tfmixanima1.Text;
                if (!mixanima2.Text.Equals(""))
                    history.MixanimaTherismou2 = mixanima2.Text;
                if (!tfmixanima2.Text.Equals(""))
                    history.TFMixanimatos2 = tfmixanima2.Text;
                if (!production.Text.Equals(""))
                    try
                    {
                        history.Production = double.Parse(production.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την παραγωγή με αριθμό";
                        return;
                    }
                if (!productionkindseed.Text.Equals(""))
                    history.ProductionKindSeed = productionkindseed.Text;
                if (!productionkindmass.Text.Equals(""))
                    history.ProductionKindMass = productionkindmass.Text;
                if (!productionquantity.Text.Equals(""))
                    try
                    {
                        history.ProductionQuantitySeed = double.Parse(productionquantity.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                        return;
                    }
                if (!productionquantitymass.Text.Equals(""))
                    try
                    {
                        history.ProductionQuantityMass = double.Parse(productionquantitymass.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                        return;
                    }
                if (!productionbuyprice.Text.Equals(""))
                    try
                    {
                        history.ProductionBuyPriceSeed = double.Parse(productionbuyprice.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                        return;
                    }
                if (!productionbuypricemass.Text.Equals(""))
                    try
                    {
                        history.ProductionBuyPriceMass = double.Parse(productionbuypricemass.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                        return;
                    }
                if (!agriculturistApopsidate.SelectedDate.ToString().Equals(""))
                    history.agriculturistApopsiDate = agriculturistApopsidate.SelectedDate;
                if (!HarvestDate.SelectedDate.ToString().Equals(""))
                    history.HarvestDate = HarvestDate.SelectedDate;

                history.CreatedBy = HttpContext.Current.Session["username"].ToString();
                history.LastUpdatedBy = HttpContext.Current.Session["username"].ToString();

                dc.PropertyHistories.InsertOnSubmit(history);
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Υπάρχει ήδη ιστορικό για αυτό το έτος";
                    return;
                }
                GridView1.DataBind();
            }

            if (e.CommandName.Equals("EmptyInsert"))
            {
                TextBox year = GridView1.Controls[0].Controls[0].FindControl("eEtosText") as TextBox;
                TextBox OfficeSup = GridView1.Controls[0].Controls[0].FindControl("eYpeuthinosGrafeiouText") as TextBox;
                TextBox FieldSup = GridView1.Controls[0].Controls[0].FindControl("eYpeuthinosKalText") as TextBox;
                TextBox agriculturist = GridView1.Controls[0].Controls[0].FindControl("eGeoponosText") as TextBox;
                TextBox agriculturistapopsi = GridView1.Controls[0].Controls[0].FindControl("eGeopApopsiText") as TextBox;
                TextBox mixanima1 = GridView1.Controls[0].Controls[0].FindControl("eMixanima1Text") as TextBox;
                TextBox tfmixanima1 = GridView1.Controls[0].Controls[0].FindControl("eMihanima1TFText") as TextBox;
                TextBox mixanima2 = GridView1.Controls[0].Controls[0].FindControl("eMihanima2Text") as TextBox;
                TextBox tfmixanima2 = GridView1.Controls[0].Controls[0].FindControl("eMixanima2TFText") as TextBox;
                TextBox production = GridView1.Controls[0].Controls[0].FindControl("eParagogiText") as TextBox;
                TextBox productionkindseed = GridView1.Controls[0].Controls[0].FindControl("eProdSeedText") as TextBox;
                TextBox productionquantity = GridView1.Controls[0].Controls[0].FindControl("eProdSporosQuantText") as TextBox;
                TextBox productionkindprice = GridView1.Controls[0].Controls[0].FindControl("ePriceSeed") as TextBox;
                Calendar agriculturistApopsidate = GridView1.Controls[0].Controls[0].FindControl("eApopsiGeoDate") as Calendar;
                Calendar HarvestDate = GridView1.Controls[0].Controls[0].FindControl("eTherismosDate") as Calendar;
                TextBox productionkindmass = GridView1.Controls[0].Controls[0].FindControl("eProdMassText") as TextBox;
                TextBox productionquantitymass = GridView1.Controls[0].Controls[0].FindControl("eProdMassQuantText") as TextBox;
                TextBox productionbuypricemass = GridView1.Controls[0].Controls[0].FindControl("eMassPriceText") as TextBox;
                TextBox seedcode = GridView1.Controls[0].Controls[0].FindControl("SeedCodeEmpty") as TextBox;

                history.PropertyID = idd;
                if (!year.Text.Equals(""))
                {
                    try
                    {
                        history.YearDate = int.Parse(year.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                        return;
                    }
                }
                else
                {
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                    return;
                }


                if (history.YearDate < 1900)
                {
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το έτος";
                    return;
                }
                if (!seedcode.Text.Equals(""))
                    history.KodikosSporou = seedcode.Text;
                if (!OfficeSup.Text.Equals(""))
                    history.OfficeSupervisor = OfficeSup.Text;
                if (!FieldSup.Text.Equals(""))
                    history.FieldSupervisor = FieldSup.Text;
                if (!agriculturist.Text.Equals(""))
                    history.agriculturist = agriculturist.Text;
                if (!agriculturistapopsi.Text.Equals(""))
                    history.agriculturistApopsi = agriculturistapopsi.Text;
                if (!mixanima1.Text.Equals(""))
                    history.MixanimaTherismou1 = mixanima1.Text;
                if (!tfmixanima1.Text.Equals(""))
                    history.TFMixanimatos1 = tfmixanima1.Text;
                if (!mixanima2.Text.Equals(""))
                    history.MixanimaTherismou2 = mixanima2.Text;
                if (!tfmixanima2.Text.Equals(""))
                    history.TFMixanimatos2 = tfmixanima2.Text;
                if (!production.Text.Equals(""))
                    try
                    {
                        history.Production = double.Parse(production.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την παραγωγή με αριθμό";
                        return;
                    }
                if (!productionkindseed.Text.Equals(""))
                    history.ProductionKindSeed = productionkindseed.Text;
                if (!productionquantitymass.Text.Equals(""))
                    try
                    {
                        history.ProductionQuantityMass = double.Parse(productionquantity.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                        return;
                    }
               if( !productionquantity.Text.Equals(""))
                    try
                    {
                        history.ProductionQuantitySeed = double.Parse(productionquantity.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα σπόρου με αριθμό";
                        return;
                    }
                if (!productionkindprice.Text.Equals(""))
                    try
                    {
                        history.ProductionBuyPriceSeed = double.Parse(productionkindprice.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                        return;
                    }
                //          if ((!agriculturistApopsidate.SelectedDate.ToString().Equals("")) && agriculturistApopsidate.SelectedDate.Year!=1)
                history.agriculturistApopsiDate = agriculturistApopsidate.SelectedDate;
                //        if ((!HarvestDate.SelectedDate.ToString().Equals("")) && HarvestDate.SelectedDate.Year != 1)
                history.HarvestDate = HarvestDate.SelectedDate;
                if (!productionkindmass.Text.Equals(""))
                    history.ProductionKindMass = productionkindmass.Text;
                if (!productionquantitymass.Text.Equals(""))
                    try
                    {
                        history.ProductionQuantityMass = double.Parse(productionquantitymass.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την ποσότητα με αριθμό";
                        return;
                    }
                if (!productionbuypricemass.Text.Equals(""))
                    try
                    {
                        history.ProductionBuyPriceMass = double.Parse(productionbuypricemass.Text);
                    }
                    catch (Exception ex)
                    {
                        Exception ex2 = ex;
                        HttpContext.Current.Session["message"] = "Συμπληρώστε την τιμή με αριθμό";
                        return;
                    }

                history.CreatedBy = HttpContext.Current.Session["username"].ToString();
                history.LastUpdatedBy = HttpContext.Current.Session["username"].ToString();

                dc.PropertyHistories.InsertOnSubmit(history);
                dc.SubmitChanges();
                Response.Redirect("propertyupdate.aspx?property=" + id);
            }

        }

        public void agriculturistApopsiDateShow(Object sender, EventArgs e)
        {
            Button b = (Button)this.GridView1.FooterRow.FindControl("agriculturistApopsidateButton");
            b.Visible = false;

            Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("agriculturistApopsidate");
            c.Visible = true;
        }

        public void agriculturistApopsiDateHide(Object sender, EventArgs e)
        {
            Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("agriculturistApopsidate");
            c.Visible = false;

            Button b = (Button)this.GridView1.FooterRow.FindControl("agriculturistApopsidateButton");
            b.Visible = true;
            b.Text = c.SelectedDate.ToShortDateString();
        }

        public void HarvestDateShow(Object sender, EventArgs e)
        {
            Button b = (Button)this.GridView1.FooterRow.FindControl("HarvestDateButton");
            b.Visible = false;

            Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("HarvestDate");
            c.Visible = true;
        }

        public void HarvestDateHide(Object sender, EventArgs e)
        {
            Calendar c = (Calendar)this.GridView1.FooterRow.FindControl("HarvestDate");
            c.Visible = false;

            Button b = (Button)this.GridView1.FooterRow.FindControl("HarvestDateButton");
            b.Visible = true;
            b.Text = c.SelectedDate.ToShortDateString();
        }

        protected void grid_preRender(object sender, EventArgs e)
        {
            return;
           /* TableCell cell1 = GridView1.FooterRow.Cells[1];
            TableCell cell2 = GridView1.FooterRow.Cells[0];
            GridView1.FooterRow.Cells.RemoveAt(1);
            GridView1.FooterRow.Cells.RemoveAt(0);
            GridView1.FooterRow.Cells.AddAt(0, cell1);
            GridView1.FooterRow.Cells.AddAt(1, cell2);    */
            /*if (GridView1.EditIndex != -1)
            {
                if (HttpContext.Current.Session["usertype"].Equals("adm"))
                    return;
                else //is user
                {
                    TextBox YpeuthinosGrafeiou = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox21") as TextBox;
                    if (!YpeuthinosGrafeiou.Text.Equals(""))
                        YpeuthinosGrafeiou.ReadOnly = true;
                    TextBox YpeuthinosKalliergeias = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox22") as TextBox;
                    if (!YpeuthinosKalliergeias.Text.Equals(""))
                        YpeuthinosKalliergeias.ReadOnly=true;
                    TextBox GeopKalliergeias= GridView1.Rows[GridView1.EditIndex].FindControl("TextBox23") as TextBox;
                    if (!GeopKalliergeias.Text.Equals(""))
                        GeopKalliergeias.ReadOnly = true;
                    TextBox ApopsiGeop = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox24") as TextBox;
                    if (!ApopsiGeop.Text.Equals(""))
                        ApopsiGeop.ReadOnly = true;
                    TextBox MixTherismou1 = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox26") as TextBox;
                    if (!MixTherismou1.Text.Equals(""))
                        MixTherismou1.ReadOnly = true;
                    TextBox TFMix1 = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox211") as TextBox;
                    if (!TFMix1.Text.Equals(""))
                        TFMix1.ReadOnly = true;
                    TextBox MixTherismou2 = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox28") as TextBox;
                    if (!MixTherismou2.Text.Equals(""))
                        MixTherismou2.ReadOnly = true;
                    TextBox TFMix2 = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox29") as TextBox;
                    if (!TFMix2.Text.Equals(""))
                        TFMix2.ReadOnly = true;
                    TextBox ParagogiXorafiou = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox30") as TextBox;
                    if (!ParagogiXorafiou.Text.Equals(""))
                        ParagogiXorafiou.ReadOnly = true;
                    TextBox ProdKindSeed = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox312") as TextBox;
                    if (!ProdKindSeed.Text.Equals(""))
                        ProdKindSeed.ReadOnly = true;
                    TextBox ProdQuantSeed = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox314") as TextBox;
                    if (!ProdQuantSeed.Text.Equals(""))
                        ProdQuantSeed.ReadOnly = true;
                    TextBox ProdPriceSeed = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox415") as TextBox;
                    if (!ProdPriceSeed.Text.Equals(""))
                        ProdPriceSeed.ReadOnly = true;
                    TextBox ProdKindMass = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox313") as TextBox;
                    if (!ProdKindMass.Text.Equals(""))
                        ProdKindMass.ReadOnly = true;
                    TextBox ProdQuantMass = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox666") as TextBox;
                    if (!ProdQuantMass.Text.Equals(""))
                        ProdQuantMass.ReadOnly = true;
                    TextBox ProdPriceMass = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox996") as TextBox;
                    if (!ProdPriceMass.Text.Equals(""))
                        ProdPriceMass.ReadOnly = true;

                }
            }*/
        }

        private void CreateProtocolNumber()
        {
            // If all fields are filled, create the protocol number
            if (this.q.ProtocolNumber == null)
            {

                if ((!this.Calendar1.SelectedDate.ToString().Equals("")) &&
                    (!this.Calendar2.SelectedDate.ToString().Equals("")) &&
                    (!this.Calendar3.SelectedDate.ToString().Equals("")) &&
                    (!this.Calendar4.SelectedDate.ToString().Equals("")) &&
                    (!this.Calendar5.SelectedDate.ToString().Equals("")) &&
                    (!this.TextBox1.Text.Equals("")) &&
                    (!this.TextBox4.Text.Equals("")) &&
                    (!this.TextBox5.Text.Equals("")) &&
                    (!this.TextBox6.Text.Equals("")) &&
                    (!this.TextBox7.Text.Equals("")) &&
                    (!this.TextBox8.Text.Equals("")) &&
                    (!this.TextBox9.Text.Equals("")) &&
                    (!this.TextBox10.Text.Equals("")) &&
                    (!this.TextBox11.Text.Equals("")) &&
                    (!this.TextBox12.Text.Equals("")) &&
                    (!this.TextBox13.Text.Equals("")) &&
                    (!this.SeedCode1.Text.Equals("")) &&
                    (!this.SeedCode2.Text.Equals("")) &&
                    (!this.Coordinates.Text.Equals("")) &&
                    (!this.DropDownList1.SelectedItem.Value.Equals("aa")) &&
                    (!this.DropDownList2.SelectedItem.Value.Equals("bb"))
                )
                {
                    this.q.ProtocolNumber = this.q.ID.ToString() + this.TextBox7.Text.Substring(0, 3);
                    this.ProtoNum.Text = this.q.ProtocolNumber;
                }
            }
        }

        protected void GridRowUp(object sender, GridViewUpdateEventArgs e)
        {
            TextBox ParagogiXorafiou = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox30") as TextBox;
            TextBox ProdQuantSeed = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox314") as TextBox;
            TextBox ProdPriceSeed = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox415") as TextBox;
            TextBox ProdQuantMass = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox666") as TextBox;
            TextBox ProdPriceMass = GridView1.Rows[GridView1.EditIndex].FindControl("TextBox996") as TextBox;
            if (!ParagogiXorafiou.Text.Equals(""))
                try
                {
                     double.Parse(ParagogiXorafiou.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το πεδίο Παραγωγή Χωραφιού με αριθμό";
                    e.Cancel = true;
                    
                    return;
                }
            if (!ProdQuantSeed.Text.Equals(""))
                try
                {
                    double.Parse(ProdQuantSeed.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το πεδίο Ποσότητα Παραγωγής - Σπόρος με αριθμό";
                    e.Cancel = true;
                    return;
                }
            if (!ProdPriceSeed.Text.Equals(""))
                try
                {
                    double.Parse(ProdPriceSeed.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το πεδίο Τιμή Αγοράς Παραγωγής - Σπόρος με αριθμό";
                    e.Cancel = true;
                    return;
                }
            if (!ProdQuantMass.Text.Equals(""))
                try
                {
                    double.Parse(ProdQuantMass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το πεδίο Ποσότητα Παραγωγής - Μάζα με αριθμό";
                    e.Cancel = true;
                    return;
                }
            if (!ProdPriceMass.Text.Equals(""))
                try
                {
                    double.Parse(ProdPriceMass.Text);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    HttpContext.Current.Session["message"] = "Συμπληρώστε το πεδίο Τιμή Αγοράς Παραγωγής - Μάζα με αριθμό";
                    e.Cancel = true;
                    return;
                }

        }

    }
}